﻿using Learun.Util.SqlSugar;
using Learun.Util;
using System;
using System.Collections.Generic;
using System.Text;
using SqlSugar;

namespace Learun.Application.TwoDevelopment.ZZDT_EC
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2022-06-21 15:32
    /// 描 述：关联类型表
    /// </summary>
    public class ec_parallel_CableService
    {
        #region 仓储
        Repository<ec_projectEntity> _projectRepository => new Repository<ec_projectEntity>();
        Repository<ec_parallel_CableEntity> _parallelCableEntity => new Repository<ec_parallel_CableEntity>();
        #endregion
        #region 获取数据

        /// <summary>
        /// 
        /// </summary>
        /// <param name="projId"></param>
        /// <param name="MainCableId">空时，查询所有</param>
        /// <returns></returns>
        public IEnumerable<ec_parallel_CableEntity> GetList(string projId, List<string> MainCableId)
        {
            try
            {
                var ProjectEntity = new ec_projectEntity();

                //var strSql = new StringBuilder();
                //strSql.Append(@"SELECT t.* ");
                if (projId.IsEmpty())
                {
                    return null;
                }
                else
                {
                    //ProjectEntity = this.BaseRepository().FindEntity<ec_projectEntity>(projId);
                    ProjectEntity = _projectRepository.GetById(projId);
                    //strSql.Append($" FROM ec_parallel_cable_{ProjectEntity.ProjectIndex} t ");
                }
                //if (!MainCableId.IsEmpty())
                //{
                //    if (MainCableId.Contains(","))
                //    {
                //        strSql.Append($" WHERE FIND_IN_SET(t.engineerDataId,'{MainCableId}')");
                //    }
                //    else
                //    {
                //        strSql.Append($" WHERE t.engineerDataId = '{MainCableId}'");
                //    }
                //}

                //return this.BaseRepository().FindList<ec_parallel_CableEntity>(strSql.ToString());
                //return SqlSugarHelper.Db.SqlQueryable<ec_parallel_CableEntity>(strSql.ToString()).ToList();
                var res = _parallelCableEntity.AsQueryable().AS($"ec_parallel_cable_{ProjectEntity.ProjectIndex}").Where(x => MainCableId.Contains(x.engineerDataId)).ToList();
                return res;
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }



        /// <summary>
        /// 获取实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public ec_parallel_CableEntity GetEntity(string ProjectId, string keyValue)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(ProjectId))
                {
                    //return this.BaseRepository().FindEntity<ec_parallel_CableEntity>(keyValue);
                    return _parallelCableEntity.GetById(keyValue);
                }
                else
                {
                    //var ProjectEntity = this.BaseRepository().FindEntity<ec_projectEntity>(ProjectId);
                    var ProjectEntity = _projectRepository.GetById(ProjectId);
                    var strSql = new StringBuilder();
                    strSql.Append($@"SELECT * FROM ec_parallel_cable_{ProjectEntity.ProjectIndex} WHERE p_CableId = '{keyValue}' ");
                    //var obj = this.BaseRepository().FindEntity<ec_parallel_CableEntity>(strSql.ToString());
                    var obj = SqlSugarHelper.Db.Ado.SqlQuerySingle<ec_parallel_CableEntity>(strSql.ToString());
                    return obj;
                }
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }


        #endregion

        #region 提交数据
        /// <summary>
        /// 删除实体数据(cable set wire)
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void DeleteEntity(string ProjectId, string parallelCableID)
        {
            //var db = this.BaseRepository().BeginTrans();
            _parallelCableEntity.AsTenant().BeginTran();
            try
            {
                //关联类型删除时，删除工程数据关联表记录。
                if (string.IsNullOrWhiteSpace(ProjectId))
                {

                }
                else
                {
                    //var ProjectEntity = this.BaseRepository().FindEntity<ec_projectEntity>(ProjectId);
                    //db.ExecuteBySql($@" DELETE FROM ec_parallel_cable_{ProjectEntity.ProjectIndex} WHERE p_CableId ='{parallelCableID}'; ");
                    var ProjectEntity = _projectRepository.GetById(ProjectId);
                    SqlSugarHelper.Db.Ado.ExecuteCommand($@" DELETE FROM ec_parallel_cable_{ProjectEntity.ProjectIndex} WHERE p_CableId ='{parallelCableID}'; ");
                }
                //db.Commit();
                _parallelCableEntity.AsTenant().CommitTran();
            }
            catch (Exception ex)
            {
                _parallelCableEntity.AsTenant().RollbackTran();
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ProjectId"></param>
        /// <param name="keyValue"></param>
        /// <param name="entity"></param>
        public void SaveEntity(string ProjectId, ref string keyValue, ec_parallel_CableEntity entity)
        {
            try
            {
                var ProjectEntity = new ec_projectEntity();
                if (!string.IsNullOrWhiteSpace(ProjectId))
                {
                    //ProjectEntity = this.BaseRepository().FindEntity<ec_projectEntity>(ProjectId);
                    ProjectEntity = _projectRepository.GetById(ProjectId);
                }
                else
                {
                    return;
                }

                if (!string.IsNullOrEmpty(keyValue))
                {

                    entity.Modify(keyValue);
                    if (string.IsNullOrWhiteSpace(ProjectId))
                    {
                        //this.BaseRepository().Update(entity);
                        _parallelCableEntity.Update(entity);
                    }
                    else
                    {
                        //this.BaseRepository().UpdateByNoMap(entity, $"ec_parallel_cable_{ProjectEntity.ProjectIndex}", "p_CableId");
                        _parallelCableEntity.AsUpdateable(entity).AS($"ec_parallel_cable_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    }

                }
                else
                {
                    entity.Create();
                    if (string.IsNullOrWhiteSpace(ProjectId))
                    {
                        //this.BaseRepository().Insert(entity);
                        _parallelCableEntity.Insert(entity);
                    }
                    else
                    {
                        //this.BaseRepository().InsertByNoMap(entity, $"ec_parallel_cable_{ProjectEntity.ProjectIndex}");
                        _parallelCableEntity.AsInsertable(entity).AS($"ec_parallel_cable_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    }
                    keyValue = entity.p_CableId;
                }
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        #endregion

    }
}
